package com.sxr.spring.web;

import com.sxr.integration.util.JsonUtils;
import com.sxr.servlet.ServletFileService;
import com.sxr.spring.SxrFileSignatureProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 安全文件控制器
 * 提供文件下载和预览的HTTP接口
 * 基于Servlet适配器实现，支持配置化路径
 *
 * @author SXR
 * @since 1.0.0
 */
@Controller
public class SecureFileController {

    private final ServletFileService servletFileService;

    public SecureFileController(ServletFileService servletFileService) {
        this.servletFileService = servletFileService;
    }

    /**
     * 安全文件下载
     */
    @GetMapping("${sxr.file-signature.paths.download:#{'/download'}}/**")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        servletFileService.handleSecureDownload(request, response);
    }

    /**
     * 安全文件预览
     */
    @GetMapping("${sxr.file-signature.paths.preview:#{'/preview'}}/**")
    public void preview(HttpServletRequest request, HttpServletResponse response) throws IOException {
        servletFileService.handleSecurePreview(request, response);
    }

}
